package com.dtf.admin.staff.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dtf.admin.staff.form.StaffForm;
import com.dtf.admin.staff.form.SystemStaffRoleRelForm;
import com.dtf.admin.staff.service.SystemStaffService;
import com.dtf.admin.staff.vo.SystemStaff;
import com.dtf.admin.staff.vo.SystemStaffRoleRel;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.utils.Contanst;
import com.dtf.core.common.vo.Pagination;

@Controller
@RequestMapping("/admin/staff")
public class StaffController {
	
	@Resource
	SystemStaffService systemStaffService;
	
	@RequestMapping("changePassword")
	@ResponseBody
	public Map changePassword(HttpServletRequest request,
			HttpServletResponse response, StaffForm form) throws Exception {
		Map req = new HashMap();
		
		SystemStaff staff = (SystemStaff) request.getSession().getAttribute(Contanst.CURRENT_USER_KEY);
		
		if(Const.strEquals(staff.getPassword(), form.getOldPassWord())){
			req.put("code", "0");
			req.put("msg", "修改成功");
			staff.setPassword(form.getNewPassWord());
			systemStaffService.edit(staff);
		}else{
			req.put("code", "1");
			req.put("msg", "旧密码与账号密码不匹配");
		}
		return req;
	}
	
	@ResponseBody
	@RequestMapping("listStaff")
	public Map addMenu(HttpServletRequest request,
			HttpServletResponse response, StaffForm form) {
		return systemStaffService.listStaff(form);
	}
	
	@RequestMapping("edit")
	public void edit(HttpServletRequest request,
			HttpServletResponse response, StaffForm form) throws Exception {
		
		if (form.isAdd()) {
			SystemStaff staff = new SystemStaff();
			Const.copy(form, staff);
			staff.setStaff_id(Const.getUUID());
			staff.setCreate_date(new Date());
			systemStaffService.add(staff);
		}else if (form.isEdit()) {
			System.out.println(form.getSex());
		}else if (form.isDelete()) {
			String id = form.getId();
			String[] ids = id.split(",");
			SystemStaff staff = new SystemStaff();
			for (String str : ids) {
				staff.setStaff_id(str);
				systemStaffService.delete(staff);
			}
		}
		form.wirteInfo("操作成功", response);
	}
	
	@ResponseBody
	@RequestMapping("editStaff")
	public Map editStaff(HttpServletRequest request,
			HttpServletResponse response, StaffForm form) throws Exception {
		SystemStaff staff = new SystemStaff();
		Const.copy(form, staff);
		systemStaffService.edit(staff);
		Map req = new HashMap();
		req.put("code", "0");
		req.put("msg", "修改成功");
		return req;
	}
	
	@ResponseBody
	@RequestMapping("/findSystemStaffById") 
	public Map findSystemStaffById(HttpServletRequest request,
			HttpServletResponse response, StaffForm form) throws Exception {
		SystemStaff staff = new SystemStaff();
		staff.setStaff_id(form.getStaff_id());
		return systemStaffService.findSystemStaffById(form.getStaff_id());
	}
	
	@ResponseBody
	@RequestMapping("/addStaffRoleRel") 
	public Map addStaffRoleRel(HttpServletRequest request,
			HttpServletResponse response, SystemStaffRoleRelForm form) throws Exception {
		SystemStaffRoleRel rel = new SystemStaffRoleRel();
		rel.setRel_id(Const.getUUID());
		rel.setRole_id(form.getRole_id());
		rel.setStaff_id(form.getStaff_id());
		systemStaffService.add(rel);
		Map req = new HashMap();
		req.put("msg", "添加用户角色成功");
		return req;
	}
	
	@RequestMapping("/delStaffRoleRel") 
	@ResponseBody
	public Map delStaffRoleRel(HttpServletRequest request,
			HttpServletResponse response, SystemStaffRoleRelForm form) throws Exception {
		SystemStaffRoleRel rel = new SystemStaffRoleRel();
		rel.setRel_id(form.getRel_id());
		systemStaffService.delete(rel);
		Map req = new HashMap();
		req.put("msg", "删除用户角色成功");
		return req;
	}
	
	@RequestMapping("/loadStaffRolePage") 
	@ResponseBody
	public Map loadStaffRolePage(HttpServletRequest request,
			HttpServletResponse response, StaffForm form) throws Exception {
		
		String sql = "select a.staff_id,b.role_id,b.role_name,b.role_desc,a.rel_id "
				+ "from system_staff_role_rel a join system_role b on a.role_id= b.role_id "
				+ "where a.staff_id=?";
		List params = new ArrayList();
		params.add(form.getStaff_id());
		return systemStaffService.getDaoSupport().queryListMapJGPage(sql, params, form.getPageSize(), form.getPage());
	}
}
